<?php
namespace App\Model\Backstage;
use App\Model\Base;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\DB;
/**
 * Class Company
 * @package App\Model\Backstage
 *员工房源业绩统计
 * author:lxy
 * date:2018-6-09
 */
class Achievement extends Base
{
    //定义数据表名
    protected $table;
    public function __construct()
    {
        parent::__construct();
//        protected $table = 'achievement';
        $this->table = 'achievement_'.$this->branch_id;
    }
    public function getAchievementList($map = [],$where = [])
    {
        $user_list = DB::table('user')
            ->leftJoin('department', 'u_d_id', '=', 'id')
            ->where('u_b_id', $this->branch_id)
            ->where($where)
            ->where('u_status', 1)
            ->where('user.is_del', 0)
            ->select(['u_id','u_username', 'name'])
            ->get()
            ->toArray();
        foreach($user_list as $k => $v){
            $map['a_uid'] = $v->u_id;
//            $money_num = Achievement::where($map)->sum('a_money');
            $money_num = DB::table($this->table)->where($map)->sum('a_money');
            $v->money_num= $money_num;
        }
        $list = json_encode($user_list);
        $list = json_decode($list,true);
        array_multisort(array_column($list,'money_num'),SORT_DESC,$list);
        return $list;
    }




}